import {AxdrSequenceOf} from "../asn1.axdr/AxdrSequenceOf";
import {ProxySetThenGetOAD} from "./ProxySetThenGetOAD";
import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {Unsigned16} from "./Unsigned16";
import {PIID} from "./PIID";


export class SubSeqOfSetThenGets extends AxdrSequenceOf<ProxySetThenGetOAD>{

    createListElement(): ProxySetThenGetOAD {
        return new ProxySetThenGetOAD();
    }

    constructor() {
        super();
    }

    set_length(length: number) {
        super.set_length(length);
    }

}

export class ProxySetThenGetRequestList implements AxdrType{

    dataCode : Buffer | null = null;
    piid : PIID | null = null;
    waitTime : Unsigned16 | null = null;
    sets : SubSeqOfSetThenGets | null = null;

    constructor() {
    }

    setPiid(piid : PIID){
        this.piid = piid;
    }

    setWaitTime(waitTime : Unsigned16){
        this.waitTime = waitTime;
    }

    setSets(sets : SubSeqOfSetThenGets){
        this.sets = sets;
    }

    set_all(piid:PIID,waitTime:Unsigned16,sets:SubSeqOfSetThenGets){
        this.piid = piid;
        this.waitTime = waitTime;
        this.sets = sets;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;

        this.piid = new PIID();
        this.piid.setNumBit(8);
        codeLength += this.piid.decode(input);

        this.waitTime = new Unsigned16();
        this.waitTime.set_const();
        codeLength += this.waitTime.decode(input);

        this.sets = new SubSeqOfSetThenGets();
        codeLength += this.sets.decode(input);

        return codeLength;
    }

    encode(output: ReverseByteArrayOutputStream): number {

        let codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.sets != null && this.waitTime != null && this.piid != null){
                codeLength = 0;
                codeLength += this.sets.encode(output);
                codeLength += this.waitTime.encode(output);
                codeLength += this.piid.encode(output);
            }else{
                throw new Error("sets || waitTime || piid is null");
            }
        }

        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.piid != null && this.waitTime != null && this.sets != null) {
            return "sequence: {" + "piid: " + this.piid + ", waittime: " + this.waitTime + ", sets: " + this.sets + "}";
        } else {
            return "sets || waitTime || piid is null";
        }
    }

}
